<?php
/**
 * Description of comment
 *
 * @author dmitryy
 */
class Models_Comment extends Access_Db {
    function chengeActive($id)
    {
        $select = array(
            'active'
        );
        $table = 'comments';
        $where = "id='$id'";
        $this->select($select, $table, $where);
        $active = $this->getSelected();
        if($active['active'] === '0')
        {
            $this->update($table, "active=1", $where);
        } elseif($active['active'] === '1') {
            $this->update($table, "active=0", $where);
        }
    }
    
    function chengeVisible($id)
    {
        $select = array(
            'visible'
        );
        $table = 'comments';
        $where = "id='$id'";
        $this->select($select, $table, $where);
        $active = $this->getSelected();
        if($active['visible'] == '0')
        {
            $this->update($table, "visible=1", $where);
        } else {
            $this->update($table, "visible=0", $where);
        }
    }
    
    function getComment($id)
    {
        $select = array(
            'id',
            'title',
            'body',
            'name',
            'surename',
            'created',
            'active',
            'visible',
            'post'
        ); 
        $table = 'comments';
        $where = "id='$id'";
        $this->select($select, $table, $where);
        return $this->getSelected();
    }
    
    function getComments($param)
    {
        $select = array(
            'id',
            'title',
            'body',
            'name',
            'surename',
            'created',
            'active',
            'visible',
            'post'
        ); 
        $table = 'comments';
        switch ($param)
        {
            case 'admlist':
                $where = "1 order by active desc";
                break;
            case 'usr':
                $where = "visible='1' and active='1' order by created desc";
                break;
            default : 
                break;
        }
        $result = array();
        $this->select($select, $table, $where);
        while ($row = $this->getSelected())
        {
            $result[] = $row;
        }
        return $result;
    }

    function addComment($arr)
    {
        $this->prepare("INSERT INTO comments (title,body,name,surename,post) VALUES (?,?,?,?,?)");
        $this->insert($arr);
        $id = $this->getLAstInsertedID();
        $this->update("comments", "created=NOW()", "id=$id");
    }
    
    function validate($arr)
    {
        $errors = array();
        if(count($arr)>0)
        {
            foreach ($arr as $key => $value)
            {
                switch ($key) 
                {
                    case '0':
                        if($value == '')
                        {
                            $errors[] = 'Ви не ввели ім\'я';
                        }
                        break;
                    case '2':
                        if($value == '')
                        {
                            $errors[] = 'Ви не ввели заголовок';
                        }
                        break;
                    case '3':
                        if(strlen($value) < 100)
                        {
                            $errors[] = 'Повідомлення закоротке';
                        }
                        break;
                    default:
                    break;
                }
            }
            return $errors;
        }
        return FALSE;
    }
}